package com.ricohgag.entity.custom;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author ricohgag
 * @date 2018/9/19 9:17
 */
public class Pager<T> implements IPage<T> {
        private List<T> records;
        private long total;
        private long size;
        private long current;
        private String[] ascs;
        private String[] descs;
        private boolean optimizeCountSql;

        public Pager() {
            this.records = Collections.emptyList();
            this.total = 0L;
            this.size = 10L;
            this.current = 1L;
            this.optimizeCountSql = true;
        }

        public Pager(long current, long size) {
            this(current, size, 0L);
        }

        public Pager(long current, long size, Long total) {
            this.records = Collections.emptyList();
            this.total = 0L;
            this.size = 10L;
            this.current = 1L;
            this.optimizeCountSql = true;
            if (current > 1L) {
                this.current = current;
            }

            this.size = size;
            this.total = total;
        }

        public boolean hasPrevious() {
            return this.current > 1L;
        }

        public boolean hasNext() {
            return this.current < this.getPages();
        }

        @Override
        public List<T> getRecords() {
            return this.records;
        }

        @Override
        public IPage<T> setRecords(List<T> records) {
            this.records = records;
            return this;
        }

        @Override
        public long getTotal() {
            return this.total;
        }

        @Override
        public IPage<T> setTotal(long total) {
            this.total = total;
            return this;
        }
        @Override
        public long getSize() {
            return this.size;
        }
        @Override
        public IPage<T> setSize(long size) {
            this.size = size;
            return this;
        }
        @Override
        public long getCurrent() {
            return this.current;
        }
        @Override
        public IPage<T> setCurrent(long current) {
            this.current = current;
            return this;
        }
        @Override
        public String[] ascs() {
            return this.ascs;
        }

        public IPage<T> setAscs(List<String> ascs) {
            if (CollectionUtils.isNotEmpty(ascs)) {
                this.ascs = (String[])ascs.toArray(new String[0]);
            }

            return this;
        }

        public IPage<T> setAsc(String... ascs) {
            this.ascs = ascs;
            return this;
        }
        @Override
        public String[] descs() {
            return this.descs;
        }

        public IPage<T> setDescs(List<String> descs) {
            if (CollectionUtils.isNotEmpty(descs)) {
                this.descs = (String[])descs.toArray(new String[0]);
            }

            return this;
        }

        public IPage<T> setDesc(String... descs) {
            this.descs = descs;
            return this;
        }
        @Override
        public boolean optimizeCountSql() {
            return this.optimizeCountSql;
        }

        @Override
        public long getPages() {
        if (this.getSize() == 0L) {
            return 0L;
        } else {
            long pages = this.getTotal() / this.getSize();
            if (this.getTotal() % this.getSize() != 0L) {
                ++pages;
            }

            return pages;
        }
    }

        @Override
        public IPage<T> setPages(long pages) {
            return this;
        }



        public IPage<T> setOptimizeCountSql(boolean optimizeCountSql) {
            this.optimizeCountSql = optimizeCountSql;
            return this;
        }
    }

